package juc.jmm;

public class Single {

    static boolean flag = false;

    public void load() {
        while (!this.flag) {
        }
        System.out.println("线程"+Thread.currentThread().getName()+"读到啦");
    }

    public void save() {
        this.flag = true;
        System.out.println("线程"+Thread.currentThread().getName()+"修改了flag");
    }

    public static void main(String[] args) {
        Single single = new Single();
        Thread saveThread = new Thread(() -> single.save(),"saveThread");
        Thread loadThread = new Thread(() -> single.load(),"loadThread");
        loadThread.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        saveThread.start();
    }
}

